﻿using Prism.Commands;
using Xcj.Mes.Core.CustomControls;
using Xcj.Mes.WholeLine.ZH.HG.Shell.Events;

namespace Xcj.Mes.WholeLine.ZH.HG.Shell.ViewModels
{
    public class LoginViewModel : BaseViewModel
    {
        public DelegateCommand LoginCommand { get; private set; }

        private int _selectedRole = 1;
        public int SelectedRole
        {
            get => _selectedRole;
            set { SetProperty<int>(ref _selectedRole, value); }
        }

        private string _password = string.Empty;
        public string Password
        {
            get => _password;
            set { SetProperty<string>(ref _password, value); }
        }

        public LoginViewModel()
        {
            //_ = AccountModel.accountModel;
            LoginCommand = new DelegateCommand(OnLoginCommand);
        }

        private void OnLoginCommand()
        {
            if ((SelectedRole < 0) || (SelectedRole > 1))
            {
                ReportBox.Show("用户角色不能为空！");
                return;
            }

            if (string.IsNullOrEmpty(Password))
            {
                ReportBox.Show("密码不能为空！");
                return;
            }

            // 管理员
            //if (SelectedRole == 0)
            //{
            //    using (AccountService service = new AccountService())
            //    {
            //        //List<Account> list = service.All()?.OrderByDescending((o) => o.CreateTime).Where((o) => (o.Role == "管理员") && (o.Password == Password))?.Take(1).ToList();
            //        List<Account> list = service.All()?.OrderByDescending((o) => o.CreateTime).Where((o) => (o.Role == "管理员"))?.Take(1).ToList();

            //        if ((list?.Count ?? 0) <= 0)
            //        {
            //            ReportBox.Show("用户密码错误！");
            //            return;
            //        }

            //        AccountModel.accountModel.Account = (Account)list[0].Clone();
            //    }
            //}
            //else if (SelectedRole == 1) // 操作员
            //{
            //    using (AccountService service = new AccountService())
            //    {
            //        //List<Account> list = service.All()?.OrderByDescending((o) => o.CreateTime).Where((o) => (o.Role == "操作员") && (o.Password == Password))?.Take(1).ToList();
            //        List<Account> list = service.All()?.OrderByDescending((o) => o.CreateTime).Where((o) => (o.Role == "操作员"))?.Take(1).ToList();

            //        if ((list?.Count ?? 0) <= 0)
            //        {
            //            ReportBox.Show("用户密码错误！");
            //            return;
            //        }

            //        AccountModel.accountModel.Account = (Account)list[0].Clone();
            //    }
            //}
            //else
            //{
            //    ReportBox.Show("未选择用户角色！");
            //    return;
            //}

            try { _eventAggregator.GetEvent<CloseWindowEvent>().Publish(); } catch { }
        }
    }
}
